﻿using System.Web.Mvc;
using pbvs.Models.DomainModels;
using pbvs.Models.ViewModels;
using System;
using pbvs.Calculations;
using pbvs.Services.Interfaces;
using System.Collections.Generic;


namespace pbvs.Controllers
{
	public class ValuationController : Controller
	{
		private readonly IPbvsService _service;
        private readonly IReportService _report;

		public ValuationController(IPbvsService svc, IReportService report)
		{
			_service = svc;
            _report = report;
		}

		public ActionResult Index()
		{
			return View();
		}

        public ActionResult Calculate(int id)
        {
            ViewBag.Error = false;
            ValuationRequestDomainModel val = _service.LoadValuation(id);
            return View(_service.PerformValuation(val));
        }

        [HttpPost]
		public ActionResult Calculate(ValuationRequestDomainModel req)
		{
			ViewBag.Error = false;
            return View(req);
		}

        public void GetValuationPdf(int reqId)
        {
            _report.RequestReport(reqId);
        }

		public ActionResult ValuationRequest()
		{
			return PartialView();
		}

		[HttpPost]
		public ActionResult ValuationRequest(ValuationRequestDomainModel req)
		{
            try
            {
                if (_service.ProcessValuationRequest(req))
                {
					ViewBag.Error = false;
					return View("Calculate", _service.PerformValuation(req));
                }
                return View(req);
            }
            catch (Exception ex)
            {
                ViewBag.error = true;
                ViewBag.Message = ex.Message;
                ViewBag.StackTrace = ex.StackTrace;
                return View("Calculate", new Valuation());
            }
		}

        public ActionResult AttemptPreviousRequest(int id)
        {
			ViewBag.Error = false;
			ValuationRequestDomainModel req = _service.LoadValuation(id);
            return View("Calculate", _service.PerformValuation(req));
       }

        public ActionResult Review()
        {
            IEnumerable<ValuationRequestDomainModel> vals = _service.GetValuations();
            return View(vals);
        }
	}
}
